package leetcode.code0763;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import leetcode.IDebug;

public class Solution implements IDebug {

	public static final int NaN = 510;

	public List<Integer> partitionLabels(String s) {
		int[][] hash = new int[26][2];
		int m = hash.length, n = hash[0].length;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				hash[i][j] = NaN;
			}
		}
		char[] cs = s.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			int c = cs[i] - 'a';
			if (hash[c][0] == NaN) {
				hash[c][0] = i;
				hash[c][1] = i;
			} else {
				hash[c][1] = i;
			}
		}
		Arrays.sort(hash, (a, b) -> a[0] - b[0]);
		int L = 0, R = 0;
		List<Integer> ans = new ArrayList<>();
		for (int i = 0; i < hash.length && hash[i][0] != NaN; i++) {
			int[] cur = hash[i];
			int l = cur[0], r = cur[1];
			if (l > R) {
				ans.add(R - L + 1);
				L = l;
				R = r;
			} else {
				R = Math.max(r, R);
			}
		}
		ans.add(R - L + 1);
		return ans;
	}

	@Override
	public void debug4() {
		this.partitionLabels("ababcbacadefegdehijhklij");

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
